package me.healthcare.common;

import android.app.Activity;
import android.view.TextureView;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jjoe64.graphview.CustomLabelFormatter;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.GraphViewSeries;
import com.jjoe64.graphview.LineGraphView;
import com.jjoe64.graphview.GraphView.GraphViewData;

public class GraphFactory {
	public static final String BLOOD_SUGAR_UNIT = "mg/dL";
	public static final String BLOOD_PRESSURE_UNIT = "mmHg";

	public static void drawCustomGraph(String graphTitle, final String units,
			double[] records, ViewGroup layout, Activity activity) {
		int numberOfRecord = records.length;

		if (numberOfRecord < 2) {
			TextView a = new TextView(activity);
			a.setText("Chưa có đủ records để vẽ graph.");
			layout.addView(a);
			return;
		}

		GraphViewData[] data = new GraphViewData[numberOfRecord];
		for (int i = 0; i < data.length; i++) {
			data[i] = new GraphViewData(i + 1, records[i]);
		}

		GraphViewSeries exampleSeries = new GraphViewSeries(data);

		GraphView graphView = new LineGraphView(activity, graphTitle);
		graphView.setCustomLabelFormatter(new CustomLabelFormatter() {
			@Override
			public String formatLabel(double value, boolean isValueX) {
				if (!isValueX) {
					value = Math.round(value * 100.0) / 100.0;
					return value + " " + units;
				}
				if (isValueX) {
					value = Math.round(value);
					int vl = (int) value;
					return vl + "";
				}
				return null;
			}
		});

		((LineGraphView) graphView).setDrawDataPoints(true);
		((LineGraphView) graphView).setDataPointsRadius(2f);
		graphView.getGraphViewStyle().setTextSize(15);

		if (numberOfRecord > 10) {
			graphView.getGraphViewStyle().setNumHorizontalLabels(10);
			graphView.setViewPort(1, 9);
			graphView.setScrollable(true);
		} else {
			graphView.getGraphViewStyle()
					.setNumHorizontalLabels(numberOfRecord);
			graphView.setViewPort(1, numberOfRecord - 1);
			graphView.setScrollable(true);
		}
		graphView.addSeries(exampleSeries); // data

		layout.addView(graphView);
	}

	public static void drawBloodSugarGraph(double[] records, ViewGroup layout,
			Activity activity) {
		int numberOfRecord = records.length;

		if (numberOfRecord < 2) {
			TextView a = new TextView(activity);
			a.setText("Chưa có đủ records để vẽ graph.");
			layout.addView(a);
			return;
		}

		GraphViewData[] data = new GraphViewData[numberOfRecord];
		for (int i = 0; i < data.length; i++) {
			data[i] = new GraphViewData(i + 1, records[i]);
		}

		GraphViewSeries exampleSeries = new GraphViewSeries(data);

		GraphView graphView = new LineGraphView(activity, "");
		graphView.setCustomLabelFormatter(new CustomLabelFormatter() {
			@Override
			public String formatLabel(double value, boolean isValueX) {
				if (!isValueX) {
					value = Math.round(value * 100.0) / 100.0;
					return value + " " + BLOOD_SUGAR_UNIT;
				}
				if (isValueX) {
					value = Math.round(value);
					int vl = (int) value;
					return vl + "";
				}
				return null;
			}
		});

		((LineGraphView) graphView).setDrawDataPoints(true);
		((LineGraphView) graphView).setDataPointsRadius(2f);
		graphView.getGraphViewStyle().setTextSize(15);

		if (numberOfRecord > 10) {
			graphView.getGraphViewStyle().setNumHorizontalLabels(10);
			graphView.setViewPort(1, 9);
			graphView.setScrollable(true);
		} else {
			graphView.getGraphViewStyle()
					.setNumHorizontalLabels(numberOfRecord);
			graphView.setViewPort(1, numberOfRecord - 1);
			graphView.setScrollable(true);
		}
		graphView.addSeries(exampleSeries); // data

		layout.addView(graphView);
	}

	public static void drawBloodPressureGraph(double[] records,
			ViewGroup layout, Activity activity) {
		int numberOfRecord = records.length;

		if (numberOfRecord < 2) {
			TextView a = new TextView(activity);
			a.setText("Chưa có đủ records để vẽ graph.");
			layout.addView(a);
			return;
		}

		GraphViewData[] data = new GraphViewData[numberOfRecord];
		for (int i = 0; i < data.length; i++) {
			data[i] = new GraphViewData(i + 1, records[i]);
		}

		GraphViewSeries exampleSeries = new GraphViewSeries(data);

		GraphView graphView = new LineGraphView(activity, "");
		graphView.setCustomLabelFormatter(new CustomLabelFormatter() {
			@Override
			public String formatLabel(double value, boolean isValueX) {
				if (!isValueX) {
					value = Math.round(value * 100.0) / 100.0;
					return value + " " + BLOOD_PRESSURE_UNIT;
				}
				if (isValueX) {
					value = Math.round(value);
					int vl = (int) value;
					return vl + "";
				}
				return null;
			}
		});

		((LineGraphView) graphView).setDrawDataPoints(true);
		((LineGraphView) graphView).setDataPointsRadius(2f);
		graphView.getGraphViewStyle().setTextSize(15);

		if (numberOfRecord > 10) {
			graphView.getGraphViewStyle().setNumHorizontalLabels(10);
			graphView.setViewPort(1, 9);
			graphView.setScrollable(true);
		} else {
			graphView.getGraphViewStyle()
					.setNumHorizontalLabels(numberOfRecord);
			graphView.setViewPort(1, numberOfRecord - 1);
			graphView.setScrollable(true);
		}
		graphView.addSeries(exampleSeries);

		layout.addView(graphView);
	}
}
